package com.ktg.mes.pda.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ktg.common.core.domain.APIResult;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.mes.util.LGLeftJoinQueryWrapper;
import com.ktg.mes.wm.service.IWmStockOutService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.pda.domain.PdaMenuPerson;
import com.ktg.mes.pda.service.IPdaMenuPersonService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * PDA菜单关联用户Controller
 *
 * @author byteco
 * @date 2022-12-31
 */
@RestController
@RequestMapping("/pda/pdaMenuPerson")
public class PdaMenuPersonController extends BaseController
{
    @Autowired
    private IPdaMenuPersonService pdaMenuPersonService;

    @Autowired
    private IWmStockOutService wmStockOutService;

    /**
     * 查询PDA菜单关联用户列表
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaMenuPerson:list')")
    @GetMapping("/list")
    public Result<IPage<PdaMenuPerson>> list(PdaMenuPerson pdaMenuPerson,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询PDA菜单关联用户列表",req,pdaMenuPerson);
        try {
            LGLeftJoinQueryWrapper queryWrapper = new LGLeftJoinQueryWrapper<>(pdaMenuPerson,req.getParameterMap(),pageNo,pageSize);
            IPage<JSONObject> pageList = wmStockOutService.lgPageMapList(queryWrapper);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }
    /**
     * 查询PDA菜单关联用户列表
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaMenuPerson:list')")
    @GetMapping("/list2")
    public Result<IPage<PdaMenuPerson>> list2(PdaMenuPerson pdaMenuPerson,
                                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                             HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询PDA菜单关联用户列表",req,pdaMenuPerson);
        try {
            pdaMenuPerson.setUserId(SecurityUtils.getUserId());
            LGLeftJoinQueryWrapper queryWrapper = new LGLeftJoinQueryWrapper<>(pdaMenuPerson,req.getParameterMap(),pageNo,pageSize);
            queryWrapper.orderBy("sort");
            IPage<JSONObject> pageList = wmStockOutService.lgPageMapList(queryWrapper);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出PDA菜单关联用户列表
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaMenuPerson:export')")
    @Log(title = "PDA菜单关联用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PdaMenuPerson pdaMenuPerson,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出PDA菜单关联用户列表",req,pdaMenuPerson);
        try {
            List<PdaMenuPerson> list = pdaMenuPersonService.queryList(pdaMenuPerson);
            ExcelUtil<PdaMenuPerson> util = new ExcelUtil<PdaMenuPerson>(PdaMenuPerson.class);
            util.exportExcel(response, list, "PDA菜单关联用户数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取PDA菜单关联用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaMenuPerson:query')")
    @GetMapping(value = "/{recordId}")
    public Result<PdaMenuPerson> getInfo(@PathVariable("recordId") Long recordId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取PDA菜单关联用户详细信息",req,null);
        try {
            return apiLog.ok(pdaMenuPersonService.getInfo(recordId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增PDA菜单关联用户
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaMenuPerson:add')")
    @Log(title = "PDA菜单关联用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<String> add(@RequestBody PdaMenuPerson pdaMenuPerson,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增PDA菜单关联用户",req,pdaMenuPerson);
        try {
            if (pdaMenuPersonService.insert(pdaMenuPerson)==false){
                return apiLog.fail("不可重复添加用户！");
            }else{
                return apiLog.ok("添加成功！");
            }
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改PDA菜单关联用户
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaMenuPerson:edit')")
    @Log(title = "PDA菜单关联用户", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody PdaMenuPerson pdaMenuPerson,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改PDA菜单关联用户",req,pdaMenuPerson);
        try {
            pdaMenuPersonService.update(pdaMenuPerson);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除PDA菜单关联用户
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaMenuPerson:remove')")
    @Log(title = "PDA菜单关联用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public Result<String> remove(@PathVariable Long[] recordIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除PDA菜单关联用户",req,null);
        try {
            pdaMenuPersonService.delete(recordIds);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
